package fileConfig;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class operationXml {
	public static HashMap<String, HashMap<String, String>> tables = new HashMap<String, HashMap<String, String>>();
	public static HashMap<String, String> cnsql = new HashMap<String, String>();
	String filePath;

	public operationXml(String filePath){
		this.filePath=filePath;
	}
	 
	public static void main(String[] args){
//		new operationXml("resource/creaeDatabaseAndTable.xml").readxml();
//		for (Entry<String, HashMap<String, String>> entry : tables.entrySet()) {
//			for(Entry<String, String> en:entry.getValue().entrySet()){
//				System.out.println("tableName= "+entry.getKey() + "  key="+en.getKey() + "   value=" + (en.getValue()));
//			}
//		}
//		for (Entry<String, String> entry : cnsql.entrySet()) {
//			System.out.println("key= "+entry.getKey() + "  value=" + (entry.getValue()));
//		}
	}

	/**
	 * 读取xml,支持库dom4j-1.6.1.jar
	 * 
	 * @param file
	 *            xml的文件路径
	 * @throws Exception
	 */
	public void readxml(){
		SAXReader reader = new SAXReader();
		Document document=null;
		try {
			document = reader.read(new File(filePath));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		Element root = document.getRootElement();
		Iterator it = root.elementIterator();
		while (it.hasNext()) {
			Element element = (Element) it.next();
//			String key = element.getName();
//			String value = element.attributeValue("name");
			getElementTable(element);
		}
	}

	public void getElementTable(Element element) {
		HashMap<String, String> tabkv = new HashMap<String ,String>();
		String key = element.getName();
		String value = element.attributeValue("name");
		List<Element> contactList = element.elements();
		if ("table".equals(key)) {
			for (Element e : contactList) {
//				table lb = new table();
//				lb.name = e.getName();
//				lb.type = e.attributeValue("type");
//				lb.vlauseRange = e.getStringValue();
				tabkv.put(e.getName(), e.attributeValue("type"));
			}
			tables.put(value, tabkv);
		}else if ("sqlConnect".equals(key)) {
			for (Element e : contactList) {
				cnsql.put(e.getName(), e.getStringValue());
			}
		}
	}
}